/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GraphGeneratorView.java
 *
 * Created on 2011-05-12, 20:09:32
 */

package cegg;

import cegg.graph.Graph;
import cegg.graph.export.DecisionTable;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.OutputStreamWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdesktop.application.Action;

/**
 *
 * @author pak88wz
 */
public class GraphGeneratorView extends javax.swing.JDialog {

    /** Creates new form GraphGeneratorView */
    public GraphGeneratorView(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    public GraphGeneratorView(java.awt.Frame parent) {
        super(parent);
        initComponents();
        saveDecisionTabelButton.setEnabled(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        content = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        messageLabel = new javax.swing.JLabel();
        messageContentLabel = new javax.swing.JLabel();
        openSavedGraphButton = new javax.swing.JButton();
        saveDecisionTabelButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        content.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));
        content.setName("content"); // NOI18N

        javax.swing.GroupLayout contentLayout = new javax.swing.GroupLayout(content);
        content.setLayout(contentLayout);
        contentLayout.setHorizontalGroup(
            contentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 806, Short.MAX_VALUE)
        );
        contentLayout.setVerticalGroup(
            contentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 436, Short.MAX_VALUE)
        );

        jPanel1.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));
        jPanel1.setName("jPanel1"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(cegg.CeggApp.class).getContext().getResourceMap(GraphGeneratorView.class);
        messageLabel.setText(resourceMap.getString("messageLabel.text")); // NOI18N
        messageLabel.setName("messageLabel"); // NOI18N

        messageContentLabel.setForeground(resourceMap.getColor("messageContentLabel.foreground")); // NOI18N
        messageContentLabel.setText(resourceMap.getString("messageContentLabel.text")); // NOI18N
        messageContentLabel.setName("messageContentLabel"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(cegg.CeggApp.class).getContext().getActionMap(GraphGeneratorView.class, this);
        openSavedGraphButton.setAction(actionMap.get("loadSavedGraphAction")); // NOI18N
        openSavedGraphButton.setText(resourceMap.getString("openSavedGraphButton.text")); // NOI18N
        openSavedGraphButton.setName("openSavedGraphButton"); // NOI18N

        saveDecisionTabelButton.setAction(actionMap.get("generateDecisionTable")); // NOI18N
        saveDecisionTabelButton.setText(resourceMap.getString("saveDecisionTabelButton.text")); // NOI18N
        saveDecisionTabelButton.setName("saveDecisionTabelButton"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(messageLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(messageContentLabel))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(openSavedGraphButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(saveDecisionTabelButton)))
                .addContainerGap(542, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(openSavedGraphButton)
                    .addComponent(saveDecisionTabelButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(messageLabel)
                    .addComponent(messageContentLabel))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(content, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(content, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                GraphGeneratorView dialog = new GraphGeneratorView(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    @Action
    public void loadSavedGraphAction() {
        FileInputStream fis = null;
        ObjectInputStream ois = null;
        try {
            fis = new FileInputStream("C:\\temp\\graph.ser");
            ois = new ObjectInputStream(fis);

            this.graph = (Graph) ois.readObject();

            messageContentLabel.setText("OK");

            graph.paint(content.getGraphics());

            saveDecisionTabelButton.setEnabled(true);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(GraphGeneratorView.class.getName()).log(Level.SEVERE, null, ex);
            messageContentLabel.setText(ex.getMessage());
        } catch (FileNotFoundException ex) {
            Logger.getLogger(GraphGeneratorView.class.getName()).log(Level.SEVERE, null, ex);
            messageContentLabel.setText(ex.getMessage());
        } catch (IOException ex) {
            Logger.getLogger(GraphGeneratorView.class.getName()).log(Level.SEVERE, null, ex);
            messageContentLabel.setText(ex.getMessage());
        }
    }

    @Action
    public void generateDecisionTable() {
        DecisionTable decisionTable = new DecisionTable(this.graph);
        try {
            FileWriter fw = new FileWriter("C:\\temp\\decisionTable.csv");
            fw.write(decisionTable.toString());
            fw.close();
        } catch (IOException ex) {
            Logger.getLogger(GraphGeneratorView.class.getName()).log(Level.SEVERE, null, ex);
        }
        InformationDialog info = new InformationDialog(CeggApp.getApplication().getMainFrame(), true);
        info.setInfoText("Decision table has been saved to: C:\\temp\\decisionTable.csv");
        info.setVisible(true);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel content;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel messageContentLabel;
    private javax.swing.JLabel messageLabel;
    private javax.swing.JButton openSavedGraphButton;
    private javax.swing.JButton saveDecisionTabelButton;
    // End of variables declaration//GEN-END:variables
    private cegg.graph.Graph graph;
}
